package shapes;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * <p>Title: Project 12</p>
 * <p>Description: CS 235 Project #12
 *       Preconditions, postconditions, asserts, Javadoc.</p>
 * @author Anthony Varghese
 * @date   February 5th 2012
 * @version 1.0
 */
public class MainClient extends JFrame implements ActionListener {
	/**
	 * Data members
	 * serialVersionUID is for serialization - ignore this for now.
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Position and size of the window frame
	private static final int FRAME_TOP_LEFT_X = 200; // pixels from left edge of screen
	private static final int FRAME_TOP_LEFT_Y = 200; // pixels from top edge of screen
	private static final int FRAME_WIDTH  = 400;     // pixels wide
	private static final int FRAME_HEIGHT = 300;	 // pixels high

	private JLabel text; // Tells user what to do
	private String DESCRIPTION = "Let's draw stuff.";
	// Position and size of the description label
	private static final int LABEL_TOP_LEFT_X = 30; // pixels from left edge of window
	private static final int LABEL_TOP_LEFT_Y = 0; // pixels from top edge of window
	private static final int LABEL_WIDTH = FRAME_WIDTH - 2*30; // pixels wide
	private static final int LABEL_HEIGHT = 30;   // pixels high

	private JButton drawButton; // Allows us to draw shapes in a list
	// Position and size of the delete button
	private static final int BUTTON_TOP_LEFT_X = LABEL_TOP_LEFT_X + 150; // pixels from left edge of window
	private static final int BUTTON_TOP_LEFT_Y = LABEL_TOP_LEFT_Y; // pixels from top edge of window
	private static final int BUTTON_WIDTH = 100; // pixels wide
	private static final int BUTTON_HEIGHT = 35;   // pixels high

	
	/**
	 * The shapes array is an array of ShapeCarts
	 */
	private final int NUMBER_OF_SHAPE_CARTS = 5;
	ShapeCart shapes[] = new ShapeCart[ NUMBER_OF_SHAPE_CARTS ];

	
	/**
	 * MainClient default constructor
	 * 
	 */
	public MainClient() {
		setUpWindow();
	}

	/**
	 * MainClient constructor
	 * @param arg0 - title string
	 */
	public MainClient(String arg0) {
		super(arg0);
		setUpWindow();

	}

	/**
	 * setUpWindow - add all the doo-dads we need in the window.
	 * 
	 */
	private void setUpWindow() {
		setBounds( FRAME_TOP_LEFT_X, FRAME_TOP_LEFT_Y, FRAME_WIDTH, FRAME_HEIGHT);
		// Without the next line, the program is still running even if the window is "closed"
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		// We want to position our components - labels and buttons - ourselves. So disable
		//   any default layout managers that might try to position things
		setLayout( null );
		
		text = new JLabel( DESCRIPTION );
		text.setBounds(LABEL_TOP_LEFT_X, LABEL_TOP_LEFT_Y, LABEL_WIDTH, LABEL_HEIGHT);
		add( text );
				
		drawButton = new JButton("Draw");
		drawButton.setBounds(BUTTON_TOP_LEFT_X, BUTTON_TOP_LEFT_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
		add( drawButton );
		drawButton.addActionListener(this);

		/*
		 * set up the shape carts 
		 * 
		 * first, set up the carts to evenly divide the window area into columns
		 */
		int[] startx = new int[ NUMBER_OF_SHAPE_CARTS ];
		int[] endx   = new int[ NUMBER_OF_SHAPE_CARTS ];
		for (int i=0; i<NUMBER_OF_SHAPE_CARTS; i++){
			startx[i] =   i   * getWidth() / NUMBER_OF_SHAPE_CARTS;
			endx[i]   = (i+1) * getWidth() / NUMBER_OF_SHAPE_CARTS - 15;
		}
		int[] starty = new int[ NUMBER_OF_SHAPE_CARTS ];
		int[] endy   = new int[ NUMBER_OF_SHAPE_CARTS ];
		for (int i=0; i<NUMBER_OF_SHAPE_CARTS; i++){
			starty[i] =   70;
			endy[i]   =  getHeight() - 15;
		}

		for (int i=0; i<NUMBER_OF_SHAPE_CARTS; i++)
			shapes[i] = new ShapeCart( startx[i], starty[i], endx[i], endy[i] );
	}

	/**
	 * actionPerformed method for MainClient
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		java.util.Random random = new java.util.Random( System.currentTimeMillis() );
		
		for (int i=0; i<NUMBER_OF_SHAPE_CARTS; i++){
			// get the width and height of each shape cart
			int width  = shapes[i].getXend() - shapes[i].getXstart();
			int height = shapes[i].getYend() - shapes[i].getYstart();
			// Get 3 random points in each shape cart
			int x1 = shapes[i].getXstart() + random.nextInt( width  );
			int y1 = shapes[i].getYstart() + random.nextInt( height );

			int x2 = shapes[i].getXstart() + random.nextInt( width  );
			int y2 = shapes[i].getYstart() + random.nextInt( height );

			int x3 = shapes[i].getXstart() + random.nextInt( width  );
			int y3 = shapes[i].getYstart() + random.nextInt( height );

			// Select a random shape
			int s = random.nextInt( 3 );
			Shape sh;
			switch (s){
			case 0:	sh = new Line( x1, y1, x2, y2 ); break;
			case 1: sh = new Triangle( x1, y1, x2, y2, x3, y3 ); break;
			case 2: sh = new Rectangle( x1, y1, x2, y2 ); break;
			default: sh = null;
			}
			
			// Add the random shape to a cart
			shapes[i].add( sh );
		}
		
		repaint();
	}

	/**
	 * Paint method - this is where our shapes get drawn.
	 */
	public void paint(java.awt.Graphics graphics) {
		super.paint(graphics);
		
		
		if ( shapes == null )
			return;
		
		/**
		 * We want to get the following loop to draw all the things in
		 * the shapes array.
		 */
		for( ShapeCart s : shapes)
			if (s != null)
				s.draw(graphics);
	}

	/**
	 * main - this is where the program starts
	 * @param args
	 */
	public static void main(String[] args) {
		// Create the window and let it run
		(new MainClient("Project 12")).setVisible(true);
		
	}
}